package com.oauth.facebook.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.oauth.facebook.client.beans.FacebookModel;
import com.oauth.facebook.client.beans.FacebookUser;
import com.oauth.facebook.client.rpc.FacebookRemoteServiceAsync;
import com.oauth.facebook.client.ui.handler.StatusUpdateHandler;
import com.oauth.facebook.client.ui.tabs.MyConnectionsTab;
import com.oauth.facebook.client.ui.tabs.ProfileUpdatesTab;

public class HomePanel extends Composite {

	private static HomePanelUiBinder uiBinder = GWT.create(HomePanelUiBinder.class);

	interface HomePanelUiBinder extends UiBinder<Widget, HomePanel> {
	}

	@UiField
	HorizontalPanel hPanel1;

	@UiField
	ProfileInfoPanel infoPanel;

	@UiField
	SimplePanel s1AlignRight;

	@UiField
	Image linkedInImage;

	@UiField
	DecoratedTabPanel tabPanel;

	private FacebookRemoteServiceAsync mService;

	public HomePanel() {
		initWidget(uiBinder.createAndBindUi(this));
		hPanel1.setCellHorizontalAlignment(infoPanel, VerticalPanel.ALIGN_LEFT);
		hPanel1.setCellHorizontalAlignment(linkedInImage, VerticalPanel.ALIGN_RIGHT);
	}

	@UiHandler("linkedInImage")
	public void onClick(ClickEvent pEvent) {
		Window.open("http://www.facebook.com", "LinkedIn", "menubar=1,resizable=1");
	}

	public void setService(FacebookRemoteServiceAsync pService) {
		mService = pService;
	}

	public void setData(FacebookModel pModel) {
		FacebookUser user = pModel.getUser();
		infoPanel.setUser(user);

		infoPanel.addValueChangeHandler(new StatusUpdateHandler(mService));

		final MyConnectionsTab connectionsTab = new MyConnectionsTab(mService);
		final ProfileUpdatesTab updatesTab = new ProfileUpdatesTab(mService);
		// final InboxTab inboxTab = new InboxTab(mService);
		// final FriendSuggestionsTab suggestionTab = new
		// FriendSuggestionsTab(mService);

		tabPanel.add(updatesTab, "Profile Updates");
		// tabPanel.add(inboxTab, "Inbox");
		// tabPanel.add(suggestionTab, "Friend Suggestion's");
		tabPanel.add(connectionsTab, "My Connections");
		tabPanel.setAnimationEnabled(true);
		tabPanel.addSelectionHandler(new SelectionHandler<Integer>() {

			@Override
			public void onSelection(SelectionEvent<Integer> pEvent) {
				if (pEvent.getSelectedItem() == 0) {
					updatesTab.init();
				} else if (pEvent.getSelectedItem() == 1) {
					connectionsTab.init();
				} else {
					System.out.println(pEvent.getSelectedItem());
				}
			}
		});
	}

}
